@echo off
setlocal enabledelayedexpansion

:: 设置控制台编码为UTF-8
chcp 65001 > nul

:: 设置颜色
set "GREEN=[92m"
set "RED=[91m"
set "YELLOW=[93m"
set "BLUE=[94m"
set "MAGENTA=[95m"
set "CYAN=[96m"
set "WHITE=[97m"
set "RESET=[0m"

:: 设置项目路径
set "PROJECT_PATH=JGSY.AI.DesktopUI\JGSY.AI.DesktopUI.csproj"

:menu
cls
echo %CYAN%========================================%RESET%
echo %CYAN%    JGSY.AI 跨平台应用 - 开发工具%RESET%
echo %CYAN%========================================%RESET%
echo.
echo %WHITE%请选择要执行的操作：%RESET%
echo %BLUE%[1]%RESET% 编译应用程序 (Debug)
echo %BLUE%[2]%RESET% 编译应用程序 (Release)
echo %BLUE%[3]%RESET% 调试模式启动应用
echo %BLUE%[4]%RESET% 清理项目
echo %BLUE%[5]%RESET% 重新构建所有依赖
echo %BLUE%[6]%RESET% 代码分析
echo %BLUE%[7]%RESET% 热重载调试 (Hot Reload)
echo %BLUE%[8]%RESET% 发布应用
echo %BLUE%[9]%RESET% 检查并修复配置
echo %BLUE%[0]%RESET% 退出
echo.
set /p choice=%WHITE%请输入选项编号：%RESET%

if "%choice%"=="1" goto build_debug
if "%choice%"=="2" goto build_release
if "%choice%"=="3" goto debug_mode
if "%choice%"=="4" goto clean
if "%choice%"=="5" goto rebuild_all
if "%choice%"=="6" goto analyze
if "%choice%"=="7" goto hot_reload
if "%choice%"=="8" goto publish
if "%choice%"=="9" goto fix_config
if "%choice%"=="0" goto end
echo %RED%无效的选择，请重试%RESET%
timeout /t 2 >nul
goto menu

:check_sdk
echo %YELLOW%正在检查 .NET SDK...%RESET%
dotnet --version >nul 2>&1
if %errorlevel% neq 0 (
    echo %RED%[X] .NET SDK 未安装或不在 PATH 中%RESET%
    echo %RED%请下载并安装 .NET 9.0 SDK: https://dotnet.microsoft.com/download%RESET%
    pause
    goto menu
)
echo %GREEN%[√] .NET SDK 已安装%RESET%
for /f "tokens=*" %%a in ('dotnet --version') do set dotnet_version=%%a
echo %GREEN%版本: !dotnet_version!%RESET%
echo.
goto :eof

:build_debug
call :check_sdk
echo %YELLOW%正在编译 Debug 版本...%RESET%
dotnet build %PROJECT_PATH% --configuration Debug
if %errorlevel% neq 0 (
    echo %RED%[X] Debug 编译失败%RESET%
    pause
    goto menu
)
echo %GREEN%[√] Debug 编译成功%RESET%
echo %GREEN%输出位置: JGSY.AI.DesktopUI\bin\Debug\net9.0\JGSY.AI.DesktopUI.exe%RESET%
pause
goto menu

:build_release
call :check_sdk
echo %YELLOW%正在编译 Release 版本...%RESET%
dotnet build %PROJECT_PATH% --configuration Release
if %errorlevel% neq 0 (
    echo %RED%[X] Release 编译失败%RESET%
    pause
    goto menu
)
echo %GREEN%[√] Release 编译成功%RESET%
echo %GREEN%输出位置: JGSY.AI.DesktopUI\bin\Release\net9.0\JGSY.AI.DesktopUI.exe%RESET%
pause
goto menu

:debug_mode
call :check_sdk
echo %YELLOW%正在启动调试模式...%RESET%

:: 确保启动配置文件存在
if not exist "JGSY.AI.DesktopUI\Properties\launchSettings.json" (
    echo %YELLOW%未找到启动配置文件，正在创建...%RESET%
    
    if not exist "JGSY.AI.DesktopUI\Properties" (
        mkdir "JGSY.AI.DesktopUI\Properties"
    )
    
    echo { > "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo   "profiles": { >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo     "JGSY.AI.DesktopUI": { >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo       "commandName": "Project", >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo       "environmentVariables": { >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo         "DOTNET_ENVIRONMENT": "Development" >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo       } >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo     }, >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo     "DebugProfile": { >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo       "commandName": "Project", >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo       "environmentVariables": { >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo         "DOTNET_ENVIRONMENT": "Development", >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo         "DOTNET_EnableDiagnostics": "1" >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo       }, >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo       "nativeDebugging": true >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo     } >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo   } >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo } >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    
    echo %GREEN%[√] 启动配置文件已创建%RESET%
)

dotnet build %PROJECT_PATH% --configuration Debug
if %errorlevel% neq 0 (
    echo %RED%[X] 编译失败，无法启动调试%RESET%
    pause
    goto menu
)
echo %GREEN%[√] 正在启动调试器...%RESET%
start dotnet run --project %PROJECT_PATH% --configuration Debug --launch-profile "DebugProfile"
echo %GREEN%[√] 调试器已启动%RESET%
pause
goto menu

:clean
echo %YELLOW%正在清理项目...%RESET%
dotnet clean %PROJECT_PATH% --configuration Debug --verbosity minimal
dotnet clean %PROJECT_PATH% --configuration Release --verbosity minimal
if %errorlevel% neq 0 (
    echo %RED%[X] 清理失败%RESET%
    pause
    goto menu
)
echo %GREEN%[√] 清理完成%RESET%
pause
goto menu

:rebuild_all
call :check_sdk
echo %YELLOW%正在完全重建项目及所有依赖...%RESET%
dotnet clean JGSY.AI.CrossPlatformApp.sln --verbosity minimal
if %errorlevel% neq 0 (
    echo %RED%[X] 清理解决方案失败%RESET%
    pause
    goto menu
)
dotnet restore JGSY.AI.CrossPlatformApp.sln
if %errorlevel% neq 0 (
    echo %RED%[X] 还原包失败%RESET%
    pause
    goto menu
)
dotnet build %PROJECT_PATH% --no-incremental --configuration Debug
if %errorlevel% neq 0 (
    echo %RED%[X] 重新构建失败%RESET%
    pause
    goto menu
)
echo %GREEN%[√] 重新构建完成%RESET%
pause
goto menu

:analyze
call :check_sdk
echo %YELLOW%正在执行代码分析...%RESET%
dotnet build %PROJECT_PATH% /p:TreatWarningsAsErrors=true /warnaserror /p:EnforceCodeStyleInBuild=true
if %errorlevel% neq 0 (
    echo %RED%[X] 代码分析发现问题%RESET%
    pause
    goto menu
)
echo %GREEN%[√] 代码分析完成，未发现问题%RESET%
pause
goto menu

:hot_reload
call :check_sdk
echo %YELLOW%正在启动热重载调试模式...%RESET%

:: 确保启动配置文件存在
if not exist "JGSY.AI.DesktopUI\Properties\launchSettings.json" (
    echo %YELLOW%未找到启动配置文件，请先运行调试模式创建配置文件%RESET%
    pause
    goto menu
)

echo %CYAN%提示: 修改代码后保存，应用将自动更新%RESET%
start dotnet watch --project %PROJECT_PATH% run --configuration Debug --launch-profile "JGSY.AI.DesktopUI"
echo %GREEN%[√] 热重载模式已启动%RESET%
pause
goto menu

:publish
call :check_sdk
echo %YELLOW%正在发布应用程序...%RESET%
set /p publish_type=%WHITE%请选择发布类型 [1=单文件 2=框架依赖]: %RESET%

if "%publish_type%"=="1" (
    echo %YELLOW%正在发布为单文件...%RESET%
    dotnet publish %PROJECT_PATH% --configuration Release -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true -p:IncludeNativeLibrariesForSelfExtract=true --runtime win-x64
) else (
    echo %YELLOW%正在发布为框架依赖版本...%RESET%
    dotnet publish %PROJECT_PATH% --configuration Release --runtime win-x64
)

if %errorlevel% neq 0 (
    echo %RED%[X] 发布失败%RESET%
    pause
    goto menu
)
echo %GREEN%[√] 发布完成%RESET%
if "%publish_type%"=="1" (
    echo %GREEN%输出位置: JGSY.AI.DesktopUI\bin\Release\net9.0\win-x64\publish\JGSY.AI.DesktopUI.exe%RESET%
) else (
    echo %GREEN%输出位置: JGSY.AI.DesktopUI\bin\Release\net9.0\win-x64\publish\%RESET%
)
pause
goto menu

:fix_config
echo %YELLOW%正在检查和修复应用程序配置...%RESET%

:: 检查并创建Config目录
if not exist "JGSY.AI.DesktopUI\bin\Debug\net9.0\Config" (
    echo %YELLOW%创建 Config 目录...%RESET%
    mkdir "JGSY.AI.DesktopUI\bin\Debug\net9.0\Config"
    echo %GREEN%[√] Config 目录已创建%RESET%
)

:: 检查并创建theme-config.json
if not exist "JGSY.AI.DesktopUI\bin\Debug\net9.0\Config\theme-config.json" (
    echo %YELLOW%创建主题配置文件...%RESET%
    echo { > "JGSY.AI.DesktopUI\bin\Debug\net9.0\Config\theme-config.json"
    echo   "CurrentTheme": "PurpleGradient", >> "JGSY.AI.DesktopUI\bin\Debug\net9.0\Config\theme-config.json"
    echo   "AvailableThemes": [ >> "JGSY.AI.DesktopUI\bin\Debug\net9.0\Config\theme-config.json"
    echo     "Light", >> "JGSY.AI.DesktopUI\bin\Debug\net9.0\Config\theme-config.json"
    echo     "Dark", >> "JGSY.AI.DesktopUI\bin\Debug\net9.0\Config\theme-config.json"
    echo     "PurpleGradient", >> "JGSY.AI.DesktopUI\bin\Debug\net9.0\Config\theme-config.json"
    echo     "BlueGradient", >> "JGSY.AI.DesktopUI\bin\Debug\net9.0\Config\theme-config.json"
    echo     "GreenGradient" >> "JGSY.AI.DesktopUI\bin\Debug\net9.0\Config\theme-config.json"
    echo   ] >> "JGSY.AI.DesktopUI\bin\Debug\net9.0\Config\theme-config.json"
    echo } >> "JGSY.AI.DesktopUI\bin\Debug\net9.0\Config\theme-config.json"
    echo %GREEN%[√] 主题配置文件已创建%RESET%
)

:: 检查并创建启动配置文件
if not exist "JGSY.AI.DesktopUI\Properties\launchSettings.json" (
    echo %YELLOW%创建启动配置文件...%RESET%
    
    if not exist "JGSY.AI.DesktopUI\Properties" (
        mkdir "JGSY.AI.DesktopUI\Properties"
    )
    
    echo { > "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo   "profiles": { >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo     "JGSY.AI.DesktopUI": { >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo       "commandName": "Project", >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo       "environmentVariables": { >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo         "DOTNET_ENVIRONMENT": "Development" >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo       } >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo     }, >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo     "DebugProfile": { >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo       "commandName": "Project", >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo       "environmentVariables": { >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo         "DOTNET_ENVIRONMENT": "Development", >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo         "DOTNET_EnableDiagnostics": "1" >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo       }, >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo       "nativeDebugging": true >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo     } >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo   } >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    echo } >> "JGSY.AI.DesktopUI\Properties\launchSettings.json"
    
    echo %GREEN%[√] 启动配置文件已创建%RESET%
)

:: 修改项目文件以包含配置目录
set "project_updated="
findstr /c:"Config\**\*" "%PROJECT_PATH%" >nul
if %errorlevel% neq 0 (
    echo %YELLOW%更新项目文件以包含配置目录...%RESET%
    powershell -Command "(Get-Content '%PROJECT_PATH%') -replace '<ItemGroup>','<ItemGroup>\n    <Content Include=\"Config\**\*\">\n      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>\n    </Content>' | Set-Content '%PROJECT_PATH%'"
    set "project_updated=1"
    echo %GREEN%[√] 项目文件已更新%RESET%
)

:: 如果项目文件被修改，重新构建项目
if defined project_updated (
    echo %YELLOW%由于项目文件更新，正在重新构建项目...%RESET%
    dotnet build %PROJECT_PATH% --configuration Debug
    if %errorlevel% neq 0 (
        echo %RED%[X] 重新构建失败%RESET%
    ) else (
        echo %GREEN%[√] 重新构建成功%RESET%
    )
)

echo %GREEN%[√] 配置检查和修复完成%RESET%
pause
goto menu

:end
echo %CYAN%感谢使用 JGSY.AI 开发工具%RESET%
timeout /t 2 >nul
exit /b 0